package com.imeem.api.xml;

import java.net.MalformedURLException;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.imeem.api.data.MusicData;
import com.imeem.api.results.IResultCallback;
import com.imeem.api.results.SearchResults;

public class MusicDataResultHandler extends ResultHandler
{
   private MusicData _musicItem;
   
   public MusicDataResultHandler(IResultCallback function, Object context)
   {
      super(function, context);
   }
   
   public void startElement(String uri, String localName, String qName, Attributes atts)
   throws SAXException
   {
      super.startElement(uri, localName, qName, atts);
      
      if (0 == "items".compareTo(_element))
      {
         _data = new SearchResults<MusicData>();
      }
      else if (0 == "item".compareTo(_element))
      {
         _musicItem = new MusicData();
      }
   }
   public void characters(char[] ch, int start, int length) throws SAXException
   {
      super.characters(ch, start, length);
      
      SearchResults<MusicData> result = (SearchResults<MusicData>) _data;
      if (0 == "totalCount".compareTo(_element))
      {
         result.setTotalCount(Integer.parseInt(_currentData));
      }
      else if (0 == "title".compareTo(_element))
      {
         _musicItem.setTitle(_currentData);
      }
      else if (0 == "artist".compareTo(_element))
      {
         _musicItem.setArtist(_currentData);
      }
      else if (0 == "album".compareTo(_element))
      {
         _musicItem.setAlbum(_currentData);
      }
      else if (0 == "description".compareTo(_element))
      {
         _musicItem.setDescription(_currentData);
      }
      else if (0 == "genre".compareTo(_element))
      {
         _musicItem.setGenre(_currentData);
      }
      else if (0 == "duration".compareTo(_element))
      {
         if (null != _currentData && _currentData.length() > 0)
         {
            _musicItem.setDuration(Integer.parseInt(_currentData));
         }
      }
      else if (0 == "snocapID".compareTo(_element))
      {
         
      }
      else if (0 == "isSample".compareTo(_element))
      {
         if (null != _currentData && _currentData.length() > 0)
         {
            _musicItem.setIsSample(Boolean.parseBoolean(_currentData));
         }
      }
      else if (0 == "largeIconUrl".compareTo(_element))
      {
         
      }
      else if (0 == "iconUrl".compareTo(_element))
      {
         try
         {
            _musicItem.setIconURL(_currentData);
         }
         catch (MalformedURLException e)
         {
            //TODO: swallow? log the exception
         }
      }
      else if (0 == "musicEmbedUrl".compareTo(_element))
      {
         try
         {
            _musicItem.setMusicEmbedURL(_currentData);
         }
         catch (MalformedURLException e)
         {
            //TODO: log the exception
         }
      }
      else if (0 == "id".compareTo(_element))
      {
         _musicItem.setId(_currentData);
      }
      else if (0 == "url".compareTo(_element))
      {
         try
         {
            _musicItem.setURL(_currentData);
         }
         catch (MalformedURLException e)
         {
            // TODO: log the exception
         }
      }
   }
   
   public void endElement(String uri, String localName, String qName)
   throws SAXException
   {
      super.endElement(uri, localName, qName);
      if (0 == "item".compareTo(localName))
      {
         SearchResults<MusicData> result = (SearchResults<MusicData>) _data;
         result.addItemToResult(_musicItem);
         _musicItem = null;
      }
   }
}
